import json
import nacos
import os
redis_config = None
redis_client = None


def load_config():
    nacos_server = os.environ.get('NACOS_SERVER','10.8.0.25:8848')
    if nacos_server:
        global redis_config
        if redis_config:
            return redis_config
        # Nacos 服务器信息
        SERVER_ADDRESSES = nacos_server
        # 创建 Nacos 客户端实例
        client = nacos.NacosClient(
            SERVER_ADDRESSES, namespace=os.environ.get('NACOS_NAMESPACE', "public"))
        return client

    # 可以添加监听器来处理配置更新


def on_config_change(config):
    # 更新 Django 配置
    print(config)
    pass


def get_xu_cloud_config():
    client = load_config()
    if not client:
        return None
    data_id = "xu_yida_app_config"
    group = "DEFAULT_GROUP"
    # client.add_config_watcher(data_id, group, on_config_change)
    json_config = client.get_config(data_id, group)
    if json_config:
        xu_yida_app_config = json.loads(json_config)
        return xu_yida_app_config

